#!/bin/bash

kernver=${KERNVER:-$(uname -r)}
oss_ver=923
if which ossvermagic 2>/dev/null; then
	OSSVERMAGIC="$(which ossvermagic)"
else
	OSSVERMAGIC="$srcdir/oss/build/prototype/usr/sbin/ossvermagic"
fi

_compile_mods () {
	workdir=$1
	target=$2
	cd $workdir

	# Check if we should use REGPARM or non-REGPARM modules
	if $OSSVERMAGIC -r || /sbin/modinfo ext3|grep -q REGPARM ; then
		REGPARM=REGPARM
		rm -rf objects
		ln -s objects.regparm objects
		rm -rf modules
		ln -s modules.regparm modules
	else
		REGPARM=NOREGPARM
		rm -rf objects
		ln -s objects.noregparm objects
		rm -rf modules
		ln -s modules.noregparm modules
	fi
	cd build
	cp -f ../objects/osscore.o osscore_mainline.o
	rm -f Makefile
	sed -i "s|include /etc/oss.conf|OSSLIBDIR=$workdir|" Makefile.osscore
	ln -s Makefile.osscore Makefile
	sed -i -e "s|/usr/sbin/ossvermagic|${OSSVERMATIC}|" Makefile
	sed -i "s|include /etc/oss.conf|OSSLIBDIR=$workdir|" Makefile.tmpl
	echo Building module osscore
	make
	echo Linking the osscore module
	ld -r osscore.ko osscore_mainline.o -o $target/osscore.ko
	if test -f Module.symvers; then
		#Take generated symbol information and add it to module.inc
		echo "static const struct modversion_info ____versions[]" > osscore_symbols.inc
		echo " __attribute__((used))" >> osscore_symbols.inc
		echo "__attribute__((section(\"__versions\"))) = {" >> osscore_symbols.inc
		sed -e "s:^:{:" -e "s:\t:, \":" -e "s:\t\(.\)*:\"},:" < Module.symvers >> osscore_symbols.inc
		echo "};" >> osscore_symbols.inc
	else
		echo > osscore_symbols.inc
	fi
	for n in ../modules/*.o; do
		N=`basename $n .o`
		echo Building module $N 
		rm -f $N_mainline.o Makefile
		sed "s/MODNAME/$N/" < Makefile.tmpl > Makefile
		ln -s $n $N_mainline.o
		make
		ld -r $N.ko $N_mainline.o -o $target/$N.ko
		rm -f $N_mainline.o
		make clean
	done
}
install_module () {
	echo
	echo "Build oss package for kernel ${kernver}"
	echo "----------"
	workdir=$(mktemp -du /tmp/oss.XXXXXX)
	mkdir -p "${workdir}/src"
	cp -a /usr/lib/oss/* "${workdir}/src/"
	mv ${workdir}/src/PKGBUILD ${workdir}/
	mv ${workdir}/src/*.install ${workdir}/
	cd ${workdir}/
	if [ "${LOGNAME}" = "root" ]; then
		if [ "${user}" = "root" ]; then
			/usr/bin/makepkg -f -c --asroot || return 1
		else	
			chown ${user}:users ${workdir}
			chown ${user}:users ${workdir}/* >> /dev/null 2>&1
			su - ${user} -c "cd ${workdir} && /usr/bin/makepkg -f -c" || return 1
		fi
	else
		/usr/bin/makepkg -f -c || return 1
	fi
	echo -e '\E[37;44m'"\033[1mOk. oss ${kernver} package built succesfully. Installing ...\033[0m"
	if [ -e /usr/bin/sudo ]; then
		echo -e '\E[37;44m'"\033[1mIf it's asking for password - type YOUR password\033[0m"
		sudo /usr/bin/pacman -Udf ${workdir}/oss-*-$(uname -m).pkg.* || return 1
	else
		echo -e '\E[37;44m'"\033[1mIf it's asking for password - type root password\033[0m"
		su - root -c "/usr/bin/pacman -Udf ${workdir}/oss-*-$(uname -m).pkg.*" || return 1
	fi
  	rm -rf "${workdir}/src"
	echo -e '\E[37;44m'"\033[1mDone.\033[0m"
}
case "$1" in
    help|--help)
        echo "usage: $0 {install|remove}"
	echo "- install will try to build oss modules and install them"
        echo "- remove is removing oss package"
        ;;
    remove)
        sudo /usr/bin/pacman -Rd --noconfirm oss
        ;;
    install)
        install_module
        ;;
    *)
        ;;
esac